package com.milotnt.controller;

import com.milotnt.pojo.*;
import com.milotnt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ZhangMing [1157038410@qq.com]
 * @date 2021/8/20
 */

@Controller
public class LoginController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private CoachService coachService;
    @Autowired
    private ClassOrderService orderService;
    @Autowired
    private ClassTableService tableService;

    //主页、跳转管理员登录页面
    @RequestMapping("/")
    public String toAdminLogin() {
        return "adminLogin";
    }

    //跳转会员登录页面
    @RequestMapping("/toUserLogin")
    public String toUserLogin() {
        return "userLogin";
    }

    // 跳转教练登录页面
    @RequestMapping("/toCoachLogin")
    public String toCoachLogin(){
        return "coachLogin";
    }

    //管理员登录
    @RequestMapping("/adminLogin")
    public String adminLogin(Admin admin, Model model, HttpSession session) {
        Admin admin1 = adminService.adminLogin(admin);
        if (admin1 != null) {
            session.setAttribute("admin", admin1);
            return "redirect:total";
        }
        model.addAttribute("msg", "您输入的账号或密码有误，请重新输入!");
        return "adminLogin";
    }

    // 统计数量并跳转到管理员主页
    @RequestMapping("/total")
    public String total(Model model){
        //会员人数
        Integer memberTotal = memberService.selectTotalCount();
        model.addAttribute("memberTotal", memberTotal);

        //员工人数
        Integer employeeTotal = employeeService.selectTotalCount();
        model.addAttribute("employeeTotal", employeeTotal);

        //教练人数
        Integer coachTotal = coachService.selectTotalCount();
        model.addAttribute("coachTotal", coachTotal);

        //器材数
        Integer equipmentTotal = equipmentService.selectTotalCount();
        model.addAttribute("equipmentTotal", equipmentTotal);

        return "adminMain";
    }

    //会员登录
    @RequestMapping("/userLogin")
    public String userLogin(Member member, Model model, HttpSession session) {
        Member member1 = memberService.userLogin(member);
        if (member1 != null) {
            model.addAttribute("member", member1);
            session.setAttribute("user", member1);
            return "redirect:toUserMain";
        }
        model.addAttribute("msg", "您输入的账号或密码有误，请重新输入!");
        return "userLogin";
    }

    @RequestMapping("/user/toUserMain")
    public String toDelMain(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        // 显示当前会员的最近要开课的五节课
        List<ClassOrder> orderList = orderService.recentClass(member.getMemberAccount());
        model.addAttribute("orderList", orderList);
        return "userMain";
    }

    //跳转会员主页
    @RequestMapping("/toUserMain")
    public String toUserMain(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        // 显示当前会员的最近要开课的五节课
        List<ClassOrder> orderList = orderService.recentClass(member.getMemberAccount());
        model.addAttribute("orderList", orderList);
        return "userMain";
    }

    // 教练登录
    @RequestMapping("/coachLogin")
    public String coachLogin(Coach coach, Model model, HttpSession session){
        Coach coach1 = coachService.coachLogin(coach);
        if (coach1 != null) {
            model.addAttribute("coach", coach1);
            session.setAttribute("coach", coach1);
            return "redirect:toCoachMain";
        }
        model.addAttribute("msg", "您输入的账号或密码有误，请重新输入!");
        return "coachLogin";
    }

    // 跳转教练主页
    @RequestMapping("/toCoachMain")
    public String toCoachMain(Model model, HttpSession session) {
        Coach coach = (Coach) session.getAttribute("coach");
        model.addAttribute("coach", coach);
        // 显示当前会员的最近要开课的五节课
        List<ClassTable> classList = tableService.recentClass(coach.getCoachId());
        model.addAttribute("classList", classList);
        return "coachMain";
    }
}
